package com.app.lxx.pregnancyberth.utils;

import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.BackgroundColorSpan;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.URLSpan;
import android.widget.TextView;

/**
 * Created by Lxx on 2019/3/28.
 */
public class TextStyleUtils {

    /**
     * ForegroundColorSpan：前景色
     * BackgroundColorSpan：背景色
     * ClickableSpan：抽象类，可点击效果，重写onClick方法响应点击事件
     * URLSpan：超链接
     * MaskFilterSpan：EmbossMaskFilter浮雕效果，BlurMaskFilter模糊效果
     * RelativeSpan：文字相对大小
     * AbsoluteSpan：文字绝对大小
     * ScaleXSpan：x轴缩放
     * styleSpan：文字样式
     * TypefaceSpan：文字字体类型
     * TextApearanceSpan：文字外貌
     * UnderlineSpan：下划线
     * StrikeThroughSpan：删除线
     * SuperscriptSpan：上标
     * SubscriptSpan：下标
     * ImageSpan：图片
     */

    public void setForegroundColorSpan(TextView textView, String tvCont, int color, int start, int end) {
        SpannableString spannableString = new SpannableString(tvCont);
        ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(color);
        spannableString.setSpan(foregroundColorSpan, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        textView.setText(spannableString);
    }

    public void setStrikethroughSpan(TextView textView, String tvCont,  int start, int end) {
        SpannableString spannableString = new SpannableString(tvCont);
        StrikethroughSpan strikethroughSpan = new StrikethroughSpan();
        spannableString.setSpan(strikethroughSpan, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        textView.setText(spannableString);
    }

    public void setAbsoluteSizeSpan(TextView textView, String tvCont, int AbsoluteSize, int start, int end) {
        SpannableString spannableString = new SpannableString(tvCont);
        AbsoluteSizeSpan absoluteSizeSpan = new AbsoluteSizeSpan(AbsoluteSize, true);
        spannableString.setSpan(absoluteSizeSpan, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        textView.setText(spannableString);
    }

    public void setRelativeSizeSpan(TextView textView, String tvCont, float RelativeSize, int start, int end) {
        SpannableString spannableString = new SpannableString(tvCont);
        RelativeSizeSpan relativeSizeSpan = new RelativeSizeSpan(RelativeSize);
        spannableString.setSpan(relativeSizeSpan, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        textView.setText(spannableString);

    }

    public void setBackgroudColorSpan(TextView textView, String tvCont, int color, int start, int end) {
        SpannableString spannableString = new SpannableString(tvCont);
        BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(color);
        spannableString.setSpan(backgroundColorSpan, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        textView.setText(spannableString);
    }

    public void setClickableSpan(TextView textView, String tvCont, int start, int end,ClickableSpan clickableSpan ) {
        SpannableString spannableString = new SpannableString(tvCont);
//        ClickableSpan clickableSpan = new ClickableSpan() {
//            @Override
//            public void onClick(View widget) {
//                // Toast.makeText(MainActivity.this, "如果我是陈奕迅", Toast.LENGTH_SHORT).show();
//            }
//
//            @Override
//            public void updateDrawState(TextPaint ds) {
//                ds.setUnderlineText(false);
//            }
//        };
        spannableString.setSpan(clickableSpan, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        textView.setMovementMethod(LinkMovementMethod.getInstance());
        textView.setText(spannableString);
    }

    public void setURLSpan(TextView textView, String tvCont, String url, int start, int end) {
        SpannableString spannableString = new SpannableString(tvCont);
        URLSpan urlSpan = new URLSpan(url);
        spannableString.setSpan(urlSpan, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        textView.setMovementMethod(LinkMovementMethod.getInstance());
        textView.setText(spannableString);
    }



}
